﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EECMISWS;
using System.IO;

namespace WebConfigManager.TabPageControls
{
    public partial class PageConfigWebService : PageConfigBase
    {
        public Dictionary<string, string> DicAdress = null;

        private WSClientOption _option = new WSClientOption();
        public WSClientOption Option
        {
            get { return _option; }
        }

        public override bool LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            else
            {
                _option.Load();
            }
            DicAdress = WebSetting.DicAisinoAdress;
            return true;
        }

        public PageConfigWebService()
        {
            InitializeComponent();
        }

        private bool CheckInputting()
        {
            // 检查填写是否正确
            if (string.IsNullOrEmpty(textBoxUserName.Text))
            {
                MessageBox.Show("账号名称为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxPassWord.Text))
            {
                MessageBox.Show("密码不能为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxXTMC.Text))
            {
                MessageBox.Show("系统名称为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxURL.Text))
            {
                MessageBox.Show("接口服务地址不能为空.");
                return false;
            }

            if (string.IsNullOrEmpty(textBoxYwbhPrefix.Text))
            {
                MessageBox.Show("业务编号前缀不能为空.");
                return false;
            }

            if (metroComboBoxProvinces.SelectedItem == null
                || metroComboBoxProvinces.SelectedIndex == -1)
            {
                MessageBox.Show("没有选择接口使用所在省份");
                return false;
            }
            return true;
        }

        private void InitCombox()
        {
            metroComboBoxProvinces.Items.Clear();
            foreach (string key in DicAdress.Keys)
            {
                metroComboBoxProvinces.Items.Add(key);
            }
        }

        public override bool InitCtrls()
        {
            InitCombox();
            metroToggleEnabled.Checked = _option.Enabled;
            metroToggleSXQM.Checked = _option.UploadSXQM;
            textBoxXTMC.Text = _option.XTMC;
            textBoxUserName.Text = _option.User;
            textBoxPassWord.Text = _option.GetPassword();
            textBoxURL.Text = _option.URL;
            textBoxYwbhPrefix.Text = _option.Ywbh_Prefix;
            for (int i = 0; i < metroComboBoxProvinces.Items.Count; i++)
            {
                if (_option.Province.Equals(metroComboBoxProvinces.Items[i].ToString()))
                {
                    metroComboBoxProvinces.SelectedIndex = i;
                }
            }
            return true; 
        }

        public override bool UpdateData()
        {
            if (!CheckInputting())
            {
                return false;
            }
            _option.Enabled = metroToggleEnabled.Checked;
            _option.UploadSXQM = metroToggleSXQM.Checked;
            _option.URL = textBoxURL.Text;
            _option.User = textBoxUserName.Text;
            _option.XTMC = textBoxXTMC.Text;
            _option.MakeCiphertext(textBoxPassWord.Text);
            _option.Province = metroComboBoxProvinces.SelectedItem.ToString();
            _option.Ywbh_Prefix = textBoxYwbhPrefix.Text;
            _option.Save();
            return true;
        }

        private void metroComboBoxProvinces_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (metroComboBoxProvinces.SelectedItem == null
            || metroComboBoxProvinces.SelectedIndex == -1)
            {
                MessageBox.Show("没有选择接口使用所在省份");
                return;
            }
            string key = metroComboBoxProvinces.SelectedItem.ToString();
            textBoxURL.Text = DicAdress[key];
        }
    }
}
